package whf.framework.file;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import whf.file.entity.File;
import whf.framework.config.ApplicationConfig;
import whf.framework.entity.Entity;
import whf.framework.meta.service.ObjectServiceImp;
import whf.framework.service.Service;
import whf.framework.util.Base64;
import whf.framework.util.BeanFactory;
import whf.framework.util.StringUtils;
import whf.framework.util.Utils;

/**
 * 文件工具类
 * @author wanghaifeng
 * @create Mar 31, 2007 8:51:26 PM
 * 
 */
public class FileUtils {
	
	/**
	 * 获取文件物理路径，将根据不同文件类型
	 * @modify wanghaifeng Mar 31, 2007 8:56:38 PM
	 * @param file
	 * @param fileType
	 * @return
	 */
	public static String getFilePath(IFile file, String fileType){
		if(file == null) throw new IllegalArgumentException("File is null!");
		String url = file.getPhycialPath();
		if(StringUtils.isEmpty(fileType)) return url;
		
		if(url == null) return "";
		int p = url.lastIndexOf('.');
		if(p == -1) return "";
		String filename = url.substring(0, p);
		String ext = url.substring(p + 1);
		return filename+"."+fileType+"."+ext;
	}
	
	/**
	 * @modify wanghaifeng Mar 31, 2007 9:00:38 PM
	 * @param file
	 * @param fileType
	 * @return
	 */
	public static String getUploadFilePath(IFile file, String fileType){
		return ApplicationConfig.getInstance().getUploadDirectory()+ getFilePath(file, fileType);
	}
	
	/**
	 * @modify wanghaifeng Mar 31, 2007 11:30:26 PM
	 * @param req
	 * @param file
	 * @param fileType
	 * @return
	 * @throws Exception
	 */
	public static String getDownloadFileUrl(HttpServletRequest req, IFile file, String fileType) throws Exception {
		return req.getContextPath()+"/Download?path=" + Base64.encode(getFilePath(file, fileType));
	}
	
	public static void removeRubbishFiles() throws Exception{
		java.io.File dir = new java.io.File(ApplicationConfig.getInstance().getUploadDirectory());
		java.io.File[] files = dir.listFiles();
		List<whf.framework.meta.entity.Object> metas = ObjectServiceImp.getObjectService().findAll();
		List<IFileService> fileServices = Utils.newArrayList();
		for(Entity bo: metas){
			whf.framework.meta.entity.Object m =(whf.framework.meta.entity.Object)bo;
			Service service = BeanFactory.getService(m.toMeta());
			if(service instanceof IFileService){
				fileServices.add((IFileService)service);
			}
		}
		List<String> rubbishFiles = Utils.newArrayList();
		for(java.io.File f: files){
			String filename = f.getName();
			boolean found = false;
			for(IFileService fs: fileServices){
				IFile file = fs.findByFileName(filename);
				if(file != null){
					found = true;
					break;
				}
			}
			if(!found && filename.indexOf(".") == filename.lastIndexOf(".")){
				rubbishFiles.add(filename);
				//TODO judge the files
			}
		}
	}
	
	public static void main(String[] args) throws Exception{
		File file = new File();
		file.setPhycialPath("e:\\1.jpg");
		System.out.println(getFilePath(file, "small"));
	}
}
